<?php
/**
悬赏
 */
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RewardController extends  Controller{

    //获取全部信息
    public function AllReward(Request $request,$type){
        if($request ->isMethod('get')){
            $arr         = [];
            $data      = DB::table('reward')
                       ->leftjoin('user','reward.user_reward','=','user.user_id')
                       ->where('reward_type',$type)
                       ->orderBy('reward_id', 'DESC')
                       ->get();
           // $d = date('Y年m月d日 H:i');
            foreach ($data as $value){

                $da[]          = array(
                    'reward_id'=> $value ->reward_id,
                    'head' =>$value ->head,
                    'nickname'=>$value ->nickname,
                    'reward_name'=> $value ->reward_name,
                    'sum' => $value ->sum,
                    'sex' =>$value ->sex,
                    'reward_depict'=> $value ->reward_depict,
                    'time' =>mb_substr($value ->reward_creation_time,5,11));
            }
            if($data == true){
                return json::json('success', '获取全部的悬赏帮忙信息成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }
    }

    //获取自己的悬赏帮忙信息
    public function PersonalReward(Request $request,$type){
        if($request ->isMethod('post')){
            $arr      = [];
            $user_id     = $request ->input('user_id');
            $data          = DB::table('reward')
                ->leftjoin('user','reward.user_reward','=','user.user_id')
                ->where('user_reward',$user_id)
                ->where('reward_type',$type)
                ->orderBy('reward_id', 'DESC')
                ->get();
            foreach ($data as $value){
                if($value ->reward_image == 'undefined'){
                    $reward_image = [];
                }else{
                    $reward_image  = explode(',',$value ->reward_image);
                }
                $da[]          = array(
                    'reward_id'=>$value ->reward_id,
                    'head' =>$value ->head,
                    'sex'  =>$value ->sex,
                    'nickname'=>$value ->nickname,
                    'reward_name'=> $value ->reward_name,
                    'reward_image'=> $reward_image,
                    'sum'=>$value ->sum);
            }
            if($data == true){
                return json::json('success', '获取自己的悬赏帮忙信息成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }

        }
    }

    //通示ID查看详情
    public function SelectReward(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $reward_id     = $request ->input('reward_id');
            $data          = DB::table('reward')
                           ->leftjoin('user','reward.user_reward','=','user.user_id')
                           ->leftjoin('village','village.village_id','=','user.user_village')
                           ->where('reward_id',$reward_id)
                           ->get();
            foreach ($data as $value){

                if($value ->reward_image == 'undefined'){
                    $reward_image = [];
                }else{
                    $reward_image  = explode(',',$value ->reward_image);
                }


                //$date = str_replace(array("年","月","日"),"-",$value->reward_creation_time);
                //$front  = substr($date,0,10);
                //$after = substr($date,11,17);
                $arr   = $value ->reward_creation_time;
                $reward_date    = $value ->reward_date;
                $time  = strtotime($arr);
                $time_date = strtotime($reward_date);
                $time_sum  = ceil(($time_date-$time)/3600/24);
                $da         = array(
                    'head' =>$value ->head,
                    'phone'=>$value ->phone,
                    'nickname'=>$value ->nickname,
                    'address' =>/*$value->user_address.*/$value->reward_position/*.$value->floor.$value->number.$value->room*/,
                    'reward_name'=> $value ->reward_name,
                    'sum' => $value ->sum,
                    'reward_depict'=> $value ->reward_depict,
                    'sign'=>$value ->sign,
                    'time' => $time_sum,
                    'reward_image'=>$reward_image
                );

            }
            if($data == true){
                return json::json('success', '获取自己的悬赏帮忙信息成功', $da);
            }else{
               return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }
    }

    //发布
    public function AddReward(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $user_id     = $request ->input('user_id');
            $reward_type = $request ->input('reward_type');
            $reward  = $request ->input('reward_image');
            $reward_name   = $request ->input('reward_name');
            $reward_position = $request->input('reward_position');
            $sum        = $request ->input('sum');
           $reward_date     = $request ->input('reward_date');
            $reward_depict   = $request ->input('reward_depict');
            $time            = date('Y-m-d H:i:s');
                $data        = DB::table('reward')
                             ->insert([
                                 'user_reward' => $user_id,
                                 'reward_type' => $reward_type,
                                   'reward_image' => $reward,
                                   'reward_name'  => $reward_name,
                                   'reward_position' => $reward_position,
                                   'sum'            => $sum,
                                   'reward_depict'   => $reward_depict,
                                    'reward_date'     => $reward_date,
                                    'reward_creation_time' =>$time
                             ]);
                if($data == true){
                    return json::json('success', '数据添加成功', $arr);
                }else{
                    return json::json('failure','由于网络或者不正当原因失败',$arr);
                }

        }else{
            return json::json('101','提交方式错误','');
        }

    }
    //修改
    public function UpdateReward(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $reward_id     = $request ->input('reward_id');
            $reward  = $request ->input('reward_image');
            $reward_name   = $request ->input('reward_name');
            $reward_position = $request->input('reward_position');
            $price           = $request ->input('price');
            $bounty          = $request ->input('bounty');
            $reward_date     = $request ->input('reward_date');
            $reward_depict   = $request ->input('reward_depict');
            $time            = date('Y-m-d H:i:s');
            $data        = DB::table('reward')
                ->where('reward_id',$reward_id)
                ->update([
                    'reward_image' => $reward,
                    'reward_name'  => $reward_name,
                    'reward_position' => $reward_position,
                    'price'            => $price,
                    'reward_depict'   => $reward_depict,
                    'bounty'            => $bounty,
                    'reward_date'     => $reward_date,
                    'reward_update_time' =>$time
                ]);
            if($data == true){
                return json::json('success', '数据修改成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }
    //删除
    public function DelReward(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $reward_id     = $request ->input('reward_id');
            $data        = DB::table('reward')
                ->where('reward_id',$reward_id)
                ->delete();
            if($data == true){
                return json::json('success', '删除成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }

}